package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.pojo.Commodity;
import com.pojo.Employee;
import com.pojo.Sell;
import com.service.CommodityService;
import com.service.EmployeeService;
import com.service.SellService;

@Controller
@RequestMapping("/sell")
public class SellController {

	@Autowired
	private SellService sellService;
	@Autowired
	private CommodityService commodityService;
	@Autowired
	private EmployeeService employeeService;

	@RequestMapping(value = "/list")
	public String listUsers(Model model) {
		List<Sell> sells = sellService.GetAllSell();
		String name;

		for (Sell s : sells) {
			name = sellService.GetCommodity(s.getSell_Cmd_Id()).getCommodity_Name();
			s.setcName(name);
			s.setPrice(sellService.GetCommodity(s.getSell_Cmd_Id()).getCommodity_Price());
			name = sellService.GetEmployee(s.getSell_Emp_Id()).getEmployee_Name();
			s.seteName(name);
			s.setPrice_count(s.getPrice() * s.getSell_Count());
			System.out.println(s);
		}
		model.addAttribute("sells", sells);
		return "/jsp/selllist";
	}

	@RequestMapping(value = "/tosell_add")
	public String toKaoqin_add(Model model) {
		model.addAttribute("sell", new Sell());
		return "/jsp/sell_add";
	}

	@RequestMapping(value = "/sell_add")
	public String Kaoqin_add(@ModelAttribute Sell sell, Model model) {
		String url = "";
		System.out.println(sell);
		boolean result1 = false;
		boolean result2 = false;
		// 找商品编号
		List<Commodity> comms = commodityService.GetAllCommodity();
		for (Commodity c : comms) {
			if (c.getCommodity_Id() == sell.getSell_Cmd_Id()) {
				result1 = true;
				// System.out.println("1"+c.getCommodity_Id()+" "+sell.getSell_Cmd_Id());
			}
		}
		System.out.println("1" + result1);
		// 找员工
		if (result1) {
			List<Employee> emp = employeeService.GetAllEmployee();
			for (Employee e : emp) {
				if (e.getEmployee_Id() == sell.getSell_Emp_Id() && e.getEmployee_Name().equals(sell.geteName()))
					result2 = true;
			}
		}
		System.out.println("2" + result2);
		// 都存在存在
		if (result1 && result2) {
			if (sellService.addSell(sell))
				url = "redirect:../sell/list";
			else {
				String error = "添加失败";
				model.addAttribute("sell", sell);
				model.addAttribute("error", error);
				url = "jsp/sell_add";
			}

		} else {
			String error = "添加失败";
			model.addAttribute("sell", sell);
			model.addAttribute("error", error);
			url = "jsp/sell_add";
		}
		return url;
	}

	@RequestMapping(value = "/delete")
	public String delete(@RequestParam Integer sid, Model model) {
		String url = "";
		System.out.println("删除" + sid);
		if (sellService.deletSellForId(sid))
			url = "redirect:../sell/list";
		else {
			String error = "删除失败";
			model.addAttribute("error", error);
			url = "redirect:../sell/list";
		}
		return url;
	}

	@RequestMapping(value = "/toupdate")
	public String toupdate(@RequestParam Integer sid, Model model) {
		model.addAttribute("sell", sellService.GetSell(sid));
		return "jsp/sell_update";
	}

	@RequestMapping(value = "/update")
	public String update(@ModelAttribute Sell sell, Model model) {
		String url = "";
		String error = "";
		System.out.println("更新" + sell);
		// 找销售员
		boolean result = false;
		List<Employee> emps = employeeService.GetAllEmployee();
		for (Employee e : emps) {
			if (e.getEmployee_Id() == sell.getSell_Emp_Id()) {
				result = true;
			}
		}
		if (result) {
			if (sellService.updateSell(sell))
				url = "redirect:../sell/list";
			else {
				error = "更新失败";
				url = "redirect:../sell/list";
			}
		}else {
			error = "员工不存在";
			url = "jsp/sell_update";
		}
		model.addAttribute("error", error);
		return url;
	}
}
